package Interfaz;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import Persistencia.DAO.DAOCompetencia;
import Persistencia.DAO.DAOUsuario;
import Persistencia.Entidad.Usuario;


/**
 * @author 
 */
public class Principal {

	protected Shell shlPrincipal;
	
	private Usuario usuario;
	
	private Principal myself;

	public Principal(int idUsuario) {
		DAOUsuario daoUsuario = new DAOUsuario();
		usuario = daoUsuario.getUsuario(idUsuario);
		myself = this;
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		
		// Centrar ventana
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlPrincipal.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;	    
		shlPrincipal.setLocation(x, y);
		
		shlPrincipal.open();
		shlPrincipal.layout();
		while (!shlPrincipal.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlPrincipal = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN);
		shlPrincipal.setMinimumSize(new Point(380, 334));
		shlPrincipal.setSize(380, 433);
		shlPrincipal.setText("Sistema");
		
		Menu menu = new Menu(shlPrincipal, SWT.BAR);
		shlPrincipal.setMenuBar(menu);
		
		MenuItem mntmUsuario = new MenuItem(menu, SWT.CASCADE);
		mntmUsuario.setText("Usuario");
		
		Menu menu_1 = new Menu(mntmUsuario);
		mntmUsuario.setMenu(menu_1);
		
		MenuItem mntmConfigurarCuenta = new MenuItem(menu_1, SWT.NONE);
		mntmConfigurarCuenta.setText("Configurar cuenta");
		
		MenuItem mntmLogout = new MenuItem(menu_1, SWT.NONE);
		mntmLogout.setText("Cerrar sesi\u00F3n");
		mntmLogout.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlPrincipal.close();
				
				try {
					CU002 iniciarSesion = new CU002();
					iniciarSesion.open();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		MenuItem mntmCompetencia = new MenuItem(menu, SWT.CASCADE);
		mntmCompetencia.setText("Competencia");
		
		Menu menu_2 = new Menu(mntmCompetencia);
		mntmCompetencia.setMenu(menu_2);
		
		MenuItem mntmMisCompetencias = new MenuItem(menu_2, SWT.NONE);
		mntmMisCompetencias.setText("Mis competencias");
		mntmMisCompetencias.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlPrincipal.setVisible(false);
				
				try {
					CU003 verCompetencias = new CU003(usuario.getId(), myself);
					verCompetencias.open();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		MenuItem mntmVerLugares = new MenuItem(menu_2, SWT.NONE);
		mntmVerLugares.setText("Ver lugares");
		
		MenuItem mntmAyuda = new MenuItem(menu, SWT.CASCADE);
		mntmAyuda.setText("Ayuda");
		
		Menu menu_3 = new Menu(mntmAyuda);
		mntmAyuda.setMenu(menu_3);
		
		MenuItem mntmVerManual = new MenuItem(menu_3, SWT.NONE);
		mntmVerManual.setText("Ver manual");
		
		MenuItem mntmAcercaDe = new MenuItem(menu_3, SWT.NONE);
		mntmAcercaDe.setText("Acerca de ...");
		
		Label lblNewLabel = new Label(shlPrincipal, SWT.NONE);
		lblNewLabel.setImage(SWTResourceManager.getImage(Principal.class, "/Interfaz/Images/default.png"));
		lblNewLabel.setBounds(21, 10, 93, 112);
		
		Label lblRegistradoComo = new Label(shlPrincipal, SWT.NONE);
		lblRegistradoComo.setBounds(133, 24, 100, 15);
		lblRegistradoComo.setText("Registrado como:");
		
		Label lblPepito = new Label(shlPrincipal, SWT.NONE);
		lblPepito.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		lblPepito.setBounds(133, 53, 230, 25);
		lblPepito.setText(usuario.getNombre() + " " + usuario.getApellido());
		
		Label lblPepitogmailcom = new Label(shlPrincipal, SWT.NONE);
		lblPepitogmailcom.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblPepitogmailcom.setBounds(133, 81, 230, 25);
		lblPepitogmailcom.setText(usuario.getDireccionEmail());
		
		Label label = new Label(shlPrincipal, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(15, 137, 344, 2);
		
		Button btnMisCompetencias = new Button(shlPrincipal, SWT.NONE);
		btnMisCompetencias.setText("Mis competencias");
		btnMisCompetencias.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		btnMisCompetencias.setBounds(101, 167, 172, 44);
		btnMisCompetencias.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlPrincipal.setVisible(false);
				
				try {
					CU003 verCompetencias = new CU003(usuario.getId(), myself);
					verCompetencias.open();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		Button btnVerLugares = new Button(shlPrincipal, SWT.NONE);
		btnVerLugares.setText("Ver lugares");
		btnVerLugares.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		btnVerLugares.setBounds(122, 235, 130, 44);
		
		Button btnLogout = new Button(shlPrincipal, SWT.NONE);
		btnLogout.setText("Cerrar sesi\u00F3n");
		btnLogout.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		btnLogout.setBounds(115, 303, 143, 44);
		btnLogout.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlPrincipal.close();
				
				try {
					CU002 iniciarSesion = new CU002();
					iniciarSesion.open();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});

	}
}
